# Properly indents a line for the original Kernighan & Richie C
# Left braces may go on their own lines.
setmacro KRC-indent 0 (indent . -255 first ifelse c="{" (up firstnb set m (\$colno) down) Mc-g Mcs-2)
# This calculates which column a line should begin on.
map Mc-g (set m 1 if c<>"#" (ifelse c="else" Mc-e (ifelse c="}" Mc-2 (Mc-f if 5 (ping 9 wleft ifelse c="else" Mcs-0 (last left ifelse c=41 Mcs-0 (ifelse c="," Mc-, Mc-;)))))))
# This sets the indentation mark one tab length forward.
map Mcs-0 (firstnb tab set m (\$colno) pong 9)
map Mc-; (pong 9 insert (}) left force s match sc-l if c="{" (force s firstnb if c="switch" (force s tab) set m (\\\$colno) pong 9) del add m 4 last if !l (left if c=":" (sub m 4)) pong 9)
map Mc-, (pong 9 insert (}) left force s match sc-l if c="{" (force s firstnb if c="switch" (force s tab) tab set m (\\\$colno) pong 9) del add m 4 last if !l (left if c=":" (sub m 4)) pong 9)
# If the line isn't blank, this checks whether a left brace reigns
# over the current line. Toggle 5 is reset if one doesn't, which
# will usually be for comments in the preprocessor code.
map Mc-f (settoggle 5 if !r (insert (}) left force s match ifelse c="{" (force s match) (resettoggle 5) del))
#map Mcs-2 (ifelse r (col (\$m)) (split col (\$m) join))
# This does the actual indenting after it is determined which column
map Mc-1 (ping 9 force s match force s firstnb setx pong 9 del gox (}))
# This finds out how to indent a right brace by finding the line
# with its matching left brace, which it aligns it with.
map Mc-2 (ping 9 force s match force s firstnb set m (\$colno) pong 9)
# This checks whether the preceding line contains an "if", an "else",
# an "else if", or a right brace. It sets toggle 5 if it doesn't.
# This is for using it in a while loop until it finds a line which
# begins in one of these ways.
map Mc-L (resettoggle 5 up firstnb if c<>"}" (if (c<>"else if") (if c<>"if" (ifelse c="else" backtab (settoggle 5)))))
# This searches for the line that will indicate how an "else" line should
# be indented. It will be aligned with it if it begins with an "if", an
# "else if" or a right brace. But if it's another "else" line, it will
# be indented one tab length to its left.
map Mc-e (ping 9 settoggle 5 while 5 Mc-L set m (\$colno) pong 9)
#Text Mode
setmacro text-format 0 (pos-line \$indcol first push pos downadd while !r (pos-line \$parcol downadd first) sety pop pos suby dec y gom repeat \$y (ifelse r join break) firstnb ifelse r (col +\$indcol) reformat)
setmacro text-return 0 (if !l (split down) insline down text-format)
setmacro text_tab 0 (ifelse cb (indent b -255 indent b t goto start firstnb a-[ reformat) (indent . .t firstnb))
setmacro text_backtab 0 (indent . -.t firstnb)
# Stores coordinates of point into $arg1 and $arg2
map a-0 (setparcol 0 title `Left Margin set to 0')
map c-1 (settvar indcol \$colno title (Indent Column set to \$colno))
# a-[ sets the left margin to the column position.
map a-[ (ifelse l a-0 sa-[)
map sa-[ (left ifelse x<\$margin (setparcol \$colno title `Left Margin Set To \$colno') (title (Can't Set Left Margin To \$colno. The Right Margin is \$margin.)) right)
# a-] sets the right margin to the column position.
map a-] (margin (\$colno) title `Right Margin set to \$colno')
# Releases the left margins
map a-= (setparcol -1 settvar indcol 0 title `Left Margin set to -1, Indent Column set to 0')
#Justification
map A-[ (ifelse cb (goto end while cb (justify left up) goto start) (justify left))
map A-] (ifelse cb (goto end while cb (justify right up) goto start) (justify right))
map A-= (ifelse cb (goto end while cb (justify center up) goto start) (justify center))
map A-@ (if !cb lineblock set m (\$colno) dec m indent b -255 indent b (\$m) unblock)
map A-j (ifelse cb (goto end first while cb (justify full up if r (up up))) (justify full))
map A-J (ifelse cb (goto end while cb (unjustify up) goto start) unjustify)
map A-i (title (Enter the number of spaces to indent.) escimm (set m ) indent b (\$m) unblock)
map A-{ (up firstnb set m (\$colno) dec m down indent . -255 indent . (\$m))
#Format
map xa-f (down firstnb ifelse r (a-= up) (a-[ up join) reformat)
#Outline
setmacro setindcol 1 (settvar indcol \$arg1)
# Calculates the left margin ($parcol) from the outline level ($lv).
setmacro calc-parcol 0 (smv m 3 add m \$lv mul m \$tabstop setparcol \$m)
setmacro indent-outline 0 (setx while ca right subx if c=41 (add lv 3) calc-parcol add x \$parcol sub x \$tabstop setindcol \$x text-format gop right)
setmacro format-outline 0 (firstnb while ca right right ifelse r (tab a-[) (wright split last tab a-[ indent-body))
setmacro indent-body 0 (push pos down first while !r (pos-line \$parcol downadd first) pop pos down reformat up last join wright indent . .ct wright)
map My-return (if !t (first while !r up down) firstnb setx first while !r downadd downadd if !r (insline insline) gox)
#Spacing
map x-1 (goto start first while cb (if r deline if !r downadd))
map x-2 (x-1 goto start while cb (down if cb (insline down)))
map A-1 (ifelse cb x-1 (Mx-p x-1 unblock pop block))
map A-2 (ifelse cb x-2 (Mx-b x-2 unblock pop block))
map xy-2 (goto start while cb (Mx-b x-2 down pop block))
map xy-2 (top while !b (Mx-b x-2 down))
map xy-1 (top while !b (Mx-p x-1 down))
map A-! (x-1 goto start while cb (if !t (firstnb if !l (insline down)) downadd))
#Proofs
map M-tab (col 40 if !r (last col +5) insert (// ))
map M-return (firstnb scanf %[0123456789] set ln (\$scanf) last split down first inc ln insert (\$ln.) up first wright if c="Show" (col +2) down)
map a-| (goto block first find Show down while cb (overwrite (|) left down) unblock)
map `a-(' ``¬''
map `a-)' ``º''
map a-nk/ ``÷''
map a-nk* ``ª''
#Deletion
map bs (ifelse l (up last ifelse l (join del) (join right)) bs)
map del (ifelse r (if !b (last ifelse l (join del) join)) del)
# These delete parts of lines and store the result in $string and in $lastdel.
map a-bs (ifelse l (up last join right) (setx dec x first string (\\\$x) delstr (\\\$string)))
map a-del (ifelse r join (scanf l delstr (\\\$scanf)))
# The following three keymappings, a-d, s-del, and s-bs, delete words.
# a-d works much like M-d in Emacs, and the other two delete entire
# strings. They each store the deleted string in $string.
# This deletes non-alphanumerics only at the beginning of a word.
# It is otherwise similar to s-del, which deletes all characters.
map a-d (while r (down firstnb) setx while !ca (right if c=32 break if r break) while ca right minus len \$colno \$x gox string \$len delstr (\$string))
# s-del deletes everything from the cursor to the start of the next word
# or, for the last word in a line, to the end of the line.
map s-del (setx sety wright if y>\$y (goy last) minus len \$colno \$x gox string \$len delstr (\$string))
# s-bs deletes everything from the end of the previous word to just before
# the cursor.
map s-bs (setx sety wleft if y<\$y (last setx wleft) minus len \$x \$colno string \$len delstr (\$string))
map Mx-b (push block first while !r (if t break up) if r down bstart while !r (down if b break) if r up bend)
map Mx-p (push block firstnb while l (up if t break firstnb) bstart down firstnb while l (down if b break firstnb) if !l up last while l (up last) bend)
#Line
map a-nk. (first deline)
map a-nk0 (split down undeline up join join)
map a-nk2 (deline downadd undeline)
map a-nk4 (wleft a-t)
map a-nk6 (a-t wright)
map a-nk8 (deline up undeline)
map a-z (undeline)
#File Requester
map f1 (arpinsfile)
map f2 (rx Diary text-mode)
map f3 (rx1 FileOp Decrypt)
map f8 (barpsave)
map f9 (rx1 FileOp Saveas)
map A-o (rx1 FileOp Replace)
map A-n (rx1 MultiFiles Load)
#Other File Operations
map c-f4 (mw- title `Change Filename' escimm (chfilename `\$filename) mw+)